﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Kawanon.Usability;
using Kawanon.Usability.Visualization;
using Microsoft.Win32;
using System.Diagnostics;
using QTPlugin;

namespace Kawanon.QtHoney
{
	public partial class OptionsForm : Form
	{
		private IPluginServer m_server;

		public OptionsForm(IPluginServer server)
		{
			InitializeComponent();

			m_server = server;
		}

		private void toolStripButton_Rendering_Click(object sender, EventArgs e)
		{
			RenderingInformation info = Persistence.GetInformation<RenderingInformation>();
			PropertiesForm form = new PropertiesForm();
			form.Text = "描画の設定";
			form.SetIcon(Resource.RenderingInformation);
			form.PropertyGrid.SelectedObject = info;
			form.ShowDialog(this);
			
			Persistence.SaveInformation(info);
		}

		private void toolStripButton_Expressions_Click(object sender, EventArgs e)
		{
			GestureForm form = new GestureForm(m_server);
			form.ShowDialog(this);
		}

		private void toolStripTextBox_MinimumDistance_Validating(object sender, CancelEventArgs e)
		{
			ToolStripTextBox edit = (ToolStripTextBox)sender;
			//if (!Array.TrueForAll(edit.Text.ToCharArray(), c => Char.IsDigit(c)))
			//{
			//}
			edit.Tag = null;

			try
			{
				uint val = UInt32.Parse(edit.Text);

				if (val > 0)
				{
					edit.Tag = val;
					return;
				}
			}
			catch (FormatException)
			{
			}

			System.Media.SystemSounds.Beep.Play();
			e.Cancel = true;

			edit.SelectAll();
		}

		private void toolStripButton_ShowDetails_CheckedChanged(object sender, EventArgs e)
		{
			ToolStripButton button = (ToolStripButton)sender;
			toolStrip2.Visible = button.Checked;
		}

		private void OptionsForm_Load(object sender, EventArgs e)
		{
			//RegistryKey key = Plugins.OpenRegistryKey(false);
			//if (key == null)
			//{
			//    return;
			//}

			//ListViewItem[] items = new ListViewItem[listView1.Items.Count];
			//listView1.Items.CopyTo(items, 0);
			//string[] names = Array.ConvertAll(items, item => (string)item.Tag);
			//foreach (ListViewItem item in listView1.Items)
			//{
			//    string name = (string)item.Tag;
			//    item.Checked = Convert.ToBoolean(key.GetValue(name, item.Checked));
			//}

			GestureInformation info = Persistence.GetInformation<GestureInformation>();

			listView1.Items[0].Checked = info.Enabled;
			listView1.Items[1].Checked = info.DrawTrails;
			toolStripTextBox_MinimumDistance.Text = info.MinimumDistance.ToString();
		}

		private void OptionsForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			ValidateChildren();

			GestureInformation info = Persistence.GetInformation<GestureInformation>();

			//foreach (ListViewItem item in listView1.Items)
			//{
			//    string name = (string)item.Tag;
			//    key.SetValue(name, item.Checked, RegistryValueKind.DWord);
			//}
			info.Enabled = listView1.Items[0].Checked;
			info.DrawTrails = listView1.Items[1].Checked;

			if (toolStripTextBox_MinimumDistance.Tag != null)
			{
				info.MinimumDistance = (uint)toolStripTextBox_MinimumDistance.Tag;
			}

			Persistence.SaveInformation(info);
		}

		private void toolStripButton_Export_Click(object sender, EventArgs e)
		{
			SaveFileDialog dlg = new SaveFileDialog();
			dlg.Filter = "レジストリ設定 (*.reg)|*.reg";

			DialogResult res = dlg.ShowDialog(this);
			if (res == DialogResult.Cancel)
			{
				return;
			}

			RegistryKey key = Plugins.OpenRegistryKey(false);

			ProcessStartInfo si = new ProcessStartInfo();
			si.FileName = "regedit";
			si.Arguments = String.Format("/e \"{0}\" \"{1}\"", dlg.FileName, key.ToString());

			try
			{
				Process proc = Process.Start(si);
				proc.WaitForExit();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

	}
}
